﻿<cfsilent>
	<cfscript>

		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		dictAdvice = getProperty("serviceFactory").getBean("senateDictionaryAdvice");
		datetimeAdvice = getProperty("serviceFactory").getBean("datetimeAdvice");

		sequenceStudentMark = getProperty("serviceFactory").getBean("sequenceStudentMark");

		teacherId = sessionAdvice.getAutherUserID();
		
		markAdvice = getProperty("serviceFactory").getBean("markAdvice");
		
		hashCode = event.getArg("Sign");
		taskId = event.getArg("TSK");
		taskType = event.getArg("TT");

		checked = false;
	
		
		
		try{
			
			if ( decrypt(hashCode, teacherId, "PBEWithMD5AndDES", "Hex") eq taskId ) {
					
				/* 载入期末成绩提交任务 */
				sql = "	SELECT
							tm.task_id,
							tm.task_prog
						FROM
							t_task_mark tm
						WHERE
							tm.task_id = :taskId ";
							
				queryObj = new Query(datasource = application.dnsSlave);
				
				queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				
				rs_task = queryObj.execute(sql=sql).getResult();
				
				
				if ( rs_task.task_prog eq "2" or rs_task.task_prog eq "3" ) {
					
					checked = true;
					
					transaction {
						
						/* 更新成绩提交状态为 3 已提交 */
						/* 设置成绩提交时间 */

						sql = "	UPDATE t_task_mark t
								SET
									t.task_prog = :taskProgress ,
									t.time_submit = :timeSubmit 
								WHERE
									t.task_id = :taskId ";
									
						updateObj = new Query(datasource = application.dnsMaster);
						
						updateObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
						updateObj.addParam(name="taskProgress", value="3", cfsqltype="cf_sql_char");
						updateObj.addParam(name="timeSubmit", value=datetimeAdvice.getCurrentDatetimeString(), cfsqltype="cf_sql_char");
						
						updateObj.execute(sql = sql);
						

						/* 转置成绩到正式成绩记录 */

						sql = "	SELECT tmm.retaking,
									tmm.mark_mode,
									tmm.mark_u,
									tmm.mark_m,
									tmm.mark_f,
							   		tmm.mark_note,
							   		tmm.task_type,
							   		tmm.task_id,
							   		tmm.scc_id,
							  		tmm.stu_id,
							   		tmm.prop_id,
							   		c.cid,
							   		tmm.term_id,
							   		tmm.tch_id,
									c.course_credit
						  		FROM t_temp_mark tmm
						 			INNER JOIN t_course c ON c.cid = tmm.cid
								WHERE
									tmm.task_id = :taskId 
								ORDER BY
									tmm.stu_id ";

						queryObj = new Query(datasource = application.dnsMaster);
						
						queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
						rs_mark = queryObj.execute(sql=sql).getResult();
						
						timeStamp = datetimeAdvice.getCurrentDatetimeString();

						for( a = 1; a LTE rs_mark.recordCount; a++ ){
							
							sequenceStudentMark.setStudentID(rs_mark['stu_id'][a]);
							sequenceStudentMark.setCourseID(rs_mark['cid'][a]);
							sequenceStudentMark.setTermID(rs_mark['term_id'][a]);
							sequenceStudentMark.setTaskType(rs_mark['task_type'][a]);
							
							markID = sequenceStudentMark.getID();
							
							sql = "	SELECT
										t.scm_id
									FROM
										t_student_mark t
									WHERE
										t.scm_id = :markId ";

							queryObj = new Query(datasource = application.dnsMaster);
							
							queryObj.addParam(name="markId", value=markID, cfsqltype="cf_sql_varchar");
							
							rs_check = queryObj.execute(sql=sql).getResult();
							
							if( rs_check.recordCount eq 0 ){

								sql = "INSERT INTO t_student_mark
												  (scm_id,
												   stu_id,
												   term_id,
												   cid,
												   prop_id,
												   retaking,
												   mark_mode,
												   mark_result,
												   mark_note,
												   mark_resit,
												   mark_renote,
												   task_type,
												   task_id,
												   tch_id,
												   mark_time,
												   mark_resit_time,
												   scc_id,
												   mark_resit_limit,
												   remarks,
												   credit_point,
												   credit, 
												   credit_point_o,
												   mark_usual,
												   mark_middle, 
												   mark_final 
												   )
												values
												  (:markID ,
												   :studentId ,
												   :termId ,
												   :courseId ,
												   :propId ,
												   :retaking ,
												   :markMode ,
												   :markResult ,
												   :markNote ,
												   :markResit ,
												   :markRenote ,
												   :taskType ,
												   :taskId ,
												   :teacherId ,
												   :markTime ,
												   :markResitTime ,
												   :chooserId ,
												   :markResitLimit,
												   :remarks, 
												   :creditPoint,
												   :credit, 
												   :creditPointO,
												   :markUsual,
												   :markMiddle, 
												   :markFinal )";
								
							}
							else {
								sql ="UPDATE t_student_mark t
										   SET 
											   t.stu_id= :studentId,
											   t.term_id = :termId,
											   t.cid= :courseId,
											   t.prop_id= :propId,
											   t.retaking= :retaking,
											   t.mark_mode= :markMode,
											   t.mark_result= :markResult,
											   t.mark_note= :markNote,
											   t.mark_resit= :markResit,
											   t.mark_renote= :markRenote,
											   t.task_type= :taskType,
											   t.task_id= :taskId,
											   t.tch_id= :teacherId,
											   t.mark_time= :markTime,
											   t.mark_resit_time= :markResitTime,
											   t.scc_id= :chooserId,
											   t.mark_resit_limit = :markResitLimit,
											   t.remarks = :remarks,
											   t.credit_point = :creditPoint,
											   t.credit = :credit, 
											   t.credit_point_o = :creditPointO, 
											   t.mark_usual = :markUsual, 
											   t.mark_middle = :markMiddle, 
											   t.mark_final = :markFinal 
										 WHERE 
										 	   t.scm_id = :markID ";

							}
							
							/* 根据备注规范 */
							mark = 0;
						
							
							
							/* 2 - 缺考 */
							if ( rs_mark['mark_note'][a] eq "2" ) {
								mark = -1;
							}
							
							/* 3 - 缓考 */
							if ( rs_mark['mark_note'][a] eq "3" ) {
								mark = -1;
							}
							
							/* 9 - 违纪 */
							if ( rs_mark['mark_note'][a] eq "9" ) {
								mark = -1;
							}
							
							
							/* 规范百分制成绩分数 */
							if ( rs_mark['mark_mode'][a] eq "0" and rs_mark['mark_note'][a] eq "1" ){
								
								if( rs_mark['prop_id'][a] eq "1" ){ //必修
								
									mark = ( rs_mark['mark_u'][a] * 20 + rs_mark['mark_m'][a] * 30 + rs_mark['mark_f'][a] * 50 )/100;
									
								}else if ( rs_mark['prop_id'][a] eq "2" ){//选修
								
									mark = ( rs_mark['mark_u'][a] * 20 + rs_mark['mark_f'][a] * 80 )/100;
									
								}
							}
							
							/* 规范二级制和五级制成绩分数 */
							if ( (rs_mark['mark_mode'][a] eq "2" or rs_mark['mark_mode'][a] eq "4")  and rs_mark['mark_note'][a] eq "1" ) {
								
									mark = rs_mark['mark_f'][a];
								
							}
							
							courseCredit = rs_mark['course_credit'][a];
							
							if( mark lt 60){
								courseCredit = 0;	
							}
						
							queryObj = new Query(datasource = application.dnsMaster);

							queryObj.addParam(name="markID", value=markID, cfsqltype="cf_sql_varchar");
							queryObj.addParam(name="studentId", value=rs_mark['stu_id'][a], cfsqltype="cf_sql_varchar");
							queryObj.addParam(name="termId", value=rs_mark['term_id'][a], cfsqltype="cf_sql_char");
							queryObj.addParam(name="courseId", value=rs_mark['cid'][a], cfsqltype="cf_sql_varchar");
							queryObj.addParam(name="propId", value=rs_mark['prop_id'][a], cfsqltype="cf_sql_char");
							queryObj.addParam(name="retaking", value=rs_mark['retaking'][a], cfsqltype="cf_sql_char");
							queryObj.addParam(name="markMode", value=rs_mark['mark_mode'][a], cfsqltype="cf_sql_char");
							queryObj.addParam(name="markResult", value=mark, cfsqltype="cf_sql_varchar");
							queryObj.addParam(name="markNote", value=rs_mark['mark_note'][a], cfsqltype="cf_sql_char");
							queryObj.addParam(name="markResit", value=-1, cfsqltype="cf_sql_varchar");
							queryObj.addParam(name="markRenote", value=1, cfsqltype="cf_sql_char");
							queryObj.addParam(name="taskType", value=rs_mark['task_type'][a], cfsqltype="cf_sql_char");
							queryObj.addParam(name="taskId", value=rs_mark['task_id'][a], cfsqltype="cf_sql_varchar");
							queryObj.addParam(name="teacherId", value=rs_mark['tch_id'][a], cfsqltype="cf_sql_varchar");
							queryObj.addParam(name="markTime", value=timeStamp, cfsqltype="cf_sql_char");
							queryObj.addParam(name="markResitTime", value=timeStamp, cfsqltype="cf_sql_char");
							queryObj.addParam(name="chooserId", value=rs_mark['scc_id'][a], cfsqltype="cf_sql_varchar");
							queryObj.addParam(name="markResitLimit", value=100, cfsqltype="cf_sql_decimal", scale=2);
							queryObj.addParam(name="remarks", value="", cfsqltype="cf_sql_varchar");
							queryObj.addParam( name="creditPoint", value=markAdvice.getMarkCredit(rs_mark['mark_mode'][a], rs_mark['course_credit'][a], mark, -1, 100), cfsqltype="cf_sql_decimal", scale=4 );
							queryObj.addParam(name="credit", value=courseCredit, cfsqltype="cf_sql_integer");
							queryObj.addParam(name="creditPointO", value=courseCredit * markAdvice.getMarkCredit(rs_mark['mark_mode'][a], rs_mark['course_credit'][a], mark, -1, 100), cfsqltype="cf_sql_varchar");
							queryObj.addParam(name="markUsual", value=rs_mark['mark_u'][a], cfsqltype="cf_sql_decimal", scale=2);
							queryObj.addParam(name="markMiddle", value=rs_mark['mark_m'][a], cfsqltype="cf_sql_decimal", scale=2);
							queryObj.addParam(name="markFinal", value=rs_mark['mark_f'][a], cfsqltype="cf_sql_decimal", scale=2);
							
							queryObj.execute(sql=sql);

						}
						
						/* 删除临时成绩记录 */
						for( b = 1; b LTE rs_mark.recordCount; b++ ){
							sql = "	DELETE FROM
										t_temp_mark t
									WHERE 
										t.scc_id = :chooserId ";

							queryObj = new Query(datasource = application.dnsMaster);
							
							queryObj.addParam(name="chooserId", value=rs_mark['scc_id'][b], cfsqltype="cf_sql_varchar");
							
							queryObj.execute(sql=sql);
						}
						
						

						
					}
					
					/* 读取正式成绩库 生成分析报告 */
					
					targetArgs = structNew();
					structInsert( targetArgs, "TSK", taskId, true );
					structInsert( targetArgs, "TT", taskType, true );
					structInsert( targetArgs, "Sign", encrypt(taskId, teacherId, "PBEWithMD5AndDES", "Hex"), true );

				}
				
			}
		
		}catch (any ecpt) {
			
		}
		
		
		/* 判断当前显示课表内容 */
		currentTab = event.getArg("TabID", "labNotice");
	
	</cfscript>
</cfsilent>

<cfif checked >
		
		<div class="UItab">
			<div class="tabLables">
				<span <cfif currentTab eq "labNotice">class="active"</cfif> id="labNotice" tabTarget="Notice">网上成绩提交</span>
			</div>
		</div>
		<script language="javascript" type="text/javascript">
			//<![CDATA[
			<!--
			$(document).ready(function() {
						
				/* init Tab */
				$("div.tabContent").hide();
				$("div#" + $("div.tabLables span.active").attr("tabTarget")).show();
				
				$("div.tabLables > span").click(function(){
					var $targetID = $(this).attr("tabTarget");
						
					$("div.tabLables span").removeClass("active");
					$(this).addClass("active");
							
					$("div.tabContent").not('[id="' + $targetID + '"]').hide();
					$("div.tabContent#" + $targetID).show();
				});
						
			});
			// -->
			//]]>
		</script>
		
		<div id="Notice" class="tabContent">
		
			<div class="noticeBlock">
				
							
				<h3>考核成绩已提交</h3>
				<p>
					<span class="img icon16x16 message"></span>
					感谢您完成课程考核成绩的网上提交工作. 如您还有其他教学任务需提交成绩, <a href="<cfoutput>#buildURL('taskMark')#</cfoutput>">提交其他任务成绩</a>.
				</p>
				<hr/>
			</div>
		
			<div class="systemNotice">
				<h3><span class="img icon16x16 info"></span>打印成绩单</h3>
				<p>
					请打印课程成绩归档记录单, 签字后交回开课学院存档. 
				</p>
				<form id="formRemove" class="formWrapper Transparent" method="post" action="<cfoutput>#buildURL('taskMarkPrint')#</cfoutput>" target="_blank">
					<input type="hidden" name="TSK" value="<cfoutput>#taskId#</cfoutput>" />
					<input type="hidden" name="TT" value="<cfoutput>#taskType#</cfoutput>" />
					<input type="hidden" name="Sign" value="<cfoutput>#encrypt(taskId, teacherId, 'PBEWithMD5AndDES', 'Hex')#</cfoutput>" />
					<div class="lable"><input class="button2" type="submit" value="打印卷面成绩登记表" /></div>
				</form>
                    
                    <form id="formRemove" class="formWrapper Transparent" method="post" action="<cfoutput>#buildURL('taskTotalMarkPrint')#</cfoutput>" target="_blank">
					<input type="hidden" name="TSK" value="<cfoutput>#taskId#</cfoutput>" />
					<input type="hidden" name="TT" value="<cfoutput>#taskType#</cfoutput>" />
					<input type="hidden" name="Sign" value="<cfoutput>#encrypt(taskId, teacherId, 'PBEWithMD5AndDES', 'Hex')#</cfoutput>" />
					<div class="lable"><input class="button2" type="submit" value="打印汇总成绩登记表" /></div>
				</form>
				
				
			</div>
		
		</div>
		
	<cfelse>
	
		<div class="systemNotice">
			<h3><span class="img icon16x16 message"></span>错误的参数</h3>
			<p>请关闭浏览器重新登录 或 检查您是否使用了正确访问方式</p>
		</div>
		
</cfif>